//
//  SASolidStageObjectBuilder.m
//  GameFrame
//
//  Created by Hiroshi Nomura on 2013/11/14.
//  Copyright (c) 2013年 Hiroshi Nomura. All rights reserved.
//

#import "SASolidStageObjectBuilder.h"
#import "matrixUtil.h"
#import "skeletonUtil.h"

@interface SASolidStageObjectBuilder()
{
    RenderedVertex*         _renderedVertex;
    TextureImage*           _textureImage;
    SkinVertex*             _skinVertex;
    Skeleton*               _skeleton;
    Joint*                  _currentJoint;
    Matrix4x4               _currentJointRotateMatrix;
    SASolidStageObject*     _solidObject;
    NSString*               _arrayString;
    NSDictionary*           _attribute;
    NSMutableArray*         _tempArray;
}

-(BOOL)m_allocateRenderedVertex;
-(BOOL)m_allocateTextureImage;
-(BOOL)m_allocateSkinVertex;
-(BOOL)m_allocateSkeleton;
-(void)m_createTextureImage:(NSString*)path;

@end

@implementation SASolidStageObjectBuilder 

-(BOOL)buildObject:(SAStageObject*)object filePath:(NSString*)path fileType:(NSString*)type
{
    if (![super buildObject:object filePath:path fileType:type]) {
        return NO;
    }
    
    _solidObject = (SASolidStageObject*)object;
    
    if(![m_parser parse]){
        return NO;
    }
    
    if (_renderedVertex != NULL) {
        _solidObject.renderedVertex = _renderedVertex;
    }
    
    if (_textureImage != NULL) {
        _solidObject.textureImage = _textureImage;
    }
    
    if (_skeleton != NULL) {
        _solidObject.skeleton = _skeleton;
    }
    
    if (_skinVertex != NULL) {
        _solidObject.skinVertex = _skinVertex;
    }
    
    return YES;
}

- (void)parser:(NSXMLParser *)parser
didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI
 qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
	_attribute = attributeDict;
	
	//For geometory
	if ([elementName isEqualToString:@"geometry"]) {
		_solidObject.objectName = (NSString*)[attributeDict objectForKey:@"id"];
		if (!self.m_allocateRenderedVertex)
            return;
		
    //For texture
	}else if ([elementName isEqualToString:@"image"]) {
		if (!self.m_allocateTextureImage)
            return;
        
		NSString* file = (NSString*)[attributeDict objectForKey:@"file"];
		NSString* type = (NSString*)[attributeDict objectForKey:@"type"];
		NSString *path = [[NSBundle mainBundle] pathForResource:file ofType:type];
		[self m_createTextureImage:path];
		
    //For Skeleton
	}else if ([elementName isEqualToString:@"skeleton"]) {
		if (!self.m_allocateSkeleton)
            return;
		
    //For SkinVertex
	}else if ([elementName isEqualToString:@"weights"]) {
		if (!self.m_allocateSkinVertex)
            return;
		
    //For Joint
	}else if ([elementName isEqualToString:@"joint_node"]) {
		_tempArray = [[NSMutableArray alloc] initWithCapacity:_skeleton->jointCount];
		
	}else if ([elementName isEqualToString:@"node"]) {
		NSString* nodeStr = (NSString*)[attributeDict objectForKey:@"id"];
		Joint* parentJoint = NULL;
		
		if ([_tempArray count] > 0) {
			NSString* parentNodeStr = (NSString*)[_tempArray lastObject];
			char* parentNodeName = (char*)[parentNodeStr cStringUsingEncoding:NSASCIIStringEncoding];
			parentJoint = searchJointByName(_skeleton, parentNodeName);
		}
		
		char* nodeName = (char*)[nodeStr cStringUsingEncoding:NSASCIIStringEncoding];
		Joint* jointPtr = searchJointByName(_skeleton, nodeName);
		if (parentJoint != NULL){
            jointPtr->parent = parentJoint;
        }
		_currentJoint = jointPtr;
        
		//root node doesn't become current joint because root node doesn't have node name.
		if (_currentJoint != NULL) {
			[_tempArray addObject:nodeStr];
		}
		
		
        //For Animation
	}else if ([elementName isEqualToString:@"animation_list"]) {
		
		
	}
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
	_arrayString = string;
}

- (void)parser:(NSXMLParser *)parser
 didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI
 qualifiedName:(NSString *)qName
{
	GLsizei arrayCount = 0;
}

@end